package com.hczt.springcloud.oauthservice;

import com.google.common.collect.Sets;
import com.hczt.springcloud.oauthservice.entity.Authority;
import com.hczt.springcloud.oauthservice.entity.Role;
import com.hczt.springcloud.oauthservice.entity.User;
import com.hczt.springcloud.oauthservice.repository.AuthorityRepository;
import com.hczt.springcloud.oauthservice.repository.RoleRepository;
import com.hczt.springcloud.oauthservice.service.UserService;
import org.springframework.boot.CommandLineRunner;

/**
 * 精诚所至，金石为开。
 * 石の上にも三年;陽気の発する所金石亦透る。
 * Faith moves mountains.
 *
 * @author marvin.ma
 * @create 2017-12-12 23:36
 * @desc 程序启动时初始化数据
 * 需要修改现有配置
 * 将本类的@Component注解放开即可
 **/
//@Component
//@AllArgsConstructor
public class InitData implements CommandLineRunner {
    private UserService userService;
    private AuthorityRepository authorityRepository;
    private RoleRepository roleRepository;

    @Override
    public void run(String... strings) throws Exception {

        //权限
        Authority authority = new Authority();
        authority.setName("查询");
        authority.setValue("query");
        authorityRepository.save(authority);

        //角色
        Role admin = new Role();
        admin.setName("管理员");
        admin.setValue("ROLE_ADMIN");
        admin.setAuthorities(Sets.newHashSet(authorityRepository.findOne(1L)));
        roleRepository.save(admin);

        Role role = new Role();
        role.setName("普通用户");
        role.setValue("ROLE_USER");
        roleRepository.save(role);


        //用户
        User fpf = new User();
        fpf.setUsername("fpf");
        fpf.setPassword("fpf");
        fpf.setRoles(Sets.newHashSet(roleRepository.findOne(1L)));
        userService.createUser(fpf);

        User wl = new User();
        wl.setUsername("wl");
        wl.setPassword("wl");
        wl.setRoles(Sets.newHashSet(roleRepository.findOne(2L)));
        userService.createUser(wl);
    }
}
